package com.hegongda.service.impl;

import com.hegongda.bean.Introduce;
import com.hegongda.bean.Nav;
import com.hegongda.bean.PageBean;
import com.hegongda.bean.Product;
import com.hegongda.dao.ProductDao;
import com.hegongda.service.ProductService;
import com.hegongda.utils.BeanFactory;
import com.hegongda.utils.JedisUtils;
import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;

import java.sql.SQLException;
import java.util.List;

public class ProductServiceImpl implements ProductService {
    private ProductDao pd = BeanFactory.newInstance(ProductDao.class);
    @Override
    public List<Product> getHost() {
        List<Product> host = null;
        try {
            host = pd.getHost();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return host;
    }

    @Override
    public List<Product> getNews() {
        List<Product> news = null;
        try {
            news = pd.getNews();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return news;
    }

    @Override
    public List<Nav> getAllNav() {
        List<Nav> allNav = null;
        Jedis jedis = JedisUtils.getJedis();
        String category =  jedis.get("category");
        try {
            if (category == null) {
                //查询MySQL数据库,取出category表数据
                allNav = pd.getAllNav();
                //数据集合,转成JSON存储到redis
                //JSONArray.fromObject(categoryList).toString();
                jedis.set("category", JSONArray.fromObject(allNav).toString());
            } else {
                //redis数据库有数据
                //category转成集合返回
                /**
                 * toList,JSON格式字符串转成集合List
                 * 参数: JSONArray对象
                 * 被转换后的集合的泛型的class对象
                 */
                // JSONArray.fromObject(category);
                allNav = JSONArray.toList( JSONArray.fromObject(category),Nav.class );
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JedisUtils.close(jedis);
        }
        return allNav;
    }

    @Override
    public Product getOneProduct(String pid) {
        Product oneProduct = null;
        try {
            oneProduct = pd.getOneProduct(pid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return oneProduct;
    }

    @Override
    public PageBean<Product> getPageProduct(String cid, int pageSize, int pagenum) {
        PageBean<Product> pb = new PageBean<Product>();
        try {
            //设置当前页数
            pb.setCurrentPage(pagenum);
            //设置每页显示条数
            pb.setPageSize(pageSize);
            //设置显示商品数据,从数据库查询
            //调用dao层,传递,当前页,每页显示条数,分类外键
            List<Product> pageProduct = pd.getPageProduct(cid, (pagenum - 1) * pageSize, pageSize);
            pb.setList(pageProduct);
            //设置商品总数量,数据库查询
            //调用dao层方法,传递分类外键
            long productCount = pd.getProductCount(cid);
            pb.setTotalCount(productCount);
            //设置总页数
            int totalPage = (int) Math.ceil(productCount * 1.0 / pageSize);
            pb.setTotalPage(totalPage);
        }catch (SQLException e){e.printStackTrace();}
        return pb;
    }

    @Override
    public PageBean<Product> foundByNamePage(String search, int pageSize, int pagenum) {
        PageBean<Product> pb = new PageBean<Product>();
        try {
            //设置当前页数
            pb.setCurrentPage(pagenum);
            //设置每页显示条数
            pb.setPageSize(pageSize);
            //设置显示商品数据,从数据库查询
            //调用dao层,传递,当前页,每页显示条数,分类外键
            List<Product> pageProduct = pd.foundByNamePage(search, (pagenum - 1) * pageSize, pageSize);
            pb.setList(pageProduct);
            //设置商品总数量,数据库查询
            //调用dao层方法,传递分类外键
            long productCount = pd.foundByNameCount(search);
            pb.setTotalCount(productCount);
            //设置总页数
            int totalPage = (int) Math.ceil(productCount * 1.0 / pageSize);
            pb.setTotalPage(totalPage);
        }catch (SQLException e){e.printStackTrace();}
        return pb;
    }

    @Override
    public Introduce getOneProductI(String iid) {
        Introduce oneProductI = null;
        try {
            oneProductI = pd.getOneProductI(iid);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return oneProductI;
    }
}
